# coding=utf-8
from rest_framework import serializers

from goods_info.models import GoodsModel
from production_info.models import BomProcessModel, BomMaterialModel
from MSB_ERP.utils.get_inventory import get_inventory


class ProcessSerializer(serializers.ModelSerializer):
    """
    生产工序的 序列化器
    """

    class Meta:
        model = BomProcessModel
        fields = '__all__'


class MaterialSerializer(serializers.ModelSerializer):
    """
    生产物料的 序列化器
    """

    cur_inventory = serializers.SerializerMethodField(read_only=True, help_text='在仓库中库存')

    class Meta:
        model = BomMaterialModel
        fields = '__all__'

    def get_cur_inventory(self,obj:BomMaterialModel):
        result = get_inventory(obj.goods.id)
        return result if result else 0

class GoodsBomSerializer(serializers.ModelSerializer):
    '''
    某一个货品的BOM表数据 序列化器
    '''
    # 当前商品BOM表中的所有的：生产物料的表
    material_list = MaterialSerializer(many=True)
    # 当前商品BOM表中的所有的：生产工序的表
    process_list = ProcessSerializer(many=True)

    class Meta:
        model = GoodsModel
        fields = ['id','number_code','name','material_list','process_list']